<?php
    class Admincp_SupportController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_validate;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Support();
            $this->_validate = new Admincp_Form_ValidateSupport();
            $this->view->headTitle('Quản lý hỗ trợ online');
        }
        
        public function preDispatch()
        {
            $auth = Zend_Auth::getInstance();
            if(!$auth->hasIdentity()) {
                if($this->_request->getActionName() != 'login')
                    $this->_redirect('admincp/index/login');
            }
        }
        
        public function indexAction()
        {
			$this->view->data = $this->_model->listItem();;
			$this->view->total = count($this->view->data);

            if($_POST['add']) {
                $this->_redirect('admincp/support/create');
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
			    $this->_model->deleteMultiItem($listId);
			    $this->_redirect($_SERVER['HTTP_REFERER']);
            }
        }
        
        public function createAction()
        {
            if($this->_request->isPost()) {
                $name = trim(strip_tags($this->_arrParam['name']));
                $phone = trim(strip_tags($this->_arrParam['phone']));
                $type_nick = $this->_arrParam['type_nick'];
                $nickname = trim(strtolower(strip_tags($this->_arrParam['nickname'])));
                $flag = true;
                
                if($this->_validate->validName($name)) {
                    $this->view->nameValue = $name;
                }
                else {
                    $this->view->errorName = 'Tên hỗ trợ từ 1-50 kí tự';
                    $flag = false;
                }
                if($this->_validate->validPhone($phone)) {
                    $this->view->phoneValue = $phone;
                }
                else {
                    $this->view->errorPhone = 'Điện thoại từ 1-15 kí tự';
                    $flag = false;
                }
                if($this->_validate->validNickname($nickname)) {
                    $this->view->nicknameValue = $nickname;
                }
                else {
                    $this->view->errorNickname = 'Nickname từ 1-50 kí tự';
                    $flag = false;
                }
                
                if($flag == true) {
                    $data = array(
                        'name' => $name,
                        'phone' => $phone,
                        'nickname' => $nickname,
                        'type_nick' => $type_nick,
                        'status' => 1, 
                    );
                    $this->_model->saveItem($data, 'create');
                    $this->_redirect('admincp/support');
                }
            }
        }

        public function editAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->view->data = $this->_model->getItem($id);
            
            if($this->_request->isPost()) {
                $name = trim(strip_tags($this->_arrParam['name']));
                $phone = trim(strip_tags($this->_arrParam['phone']));
                $type_nick = $this->_arrParam['type_nick'];
                $nickname = trim(strtolower(strip_tags($this->_arrParam['nickname'])));
                $flag = true;
                
                if($this->_validate->validName($name)) {
                    $this->view->nameValue = $name;
                }
                else {
                    $this->view->errorName = 'Tên hỗ trợ từ 1-50 kí tự';
                    $flag = false;
                }
                if($this->_validate->validPhone($phone)) {
                    $this->view->phoneValue = $phone;
                }
                else {
                    $this->view->errorPhone = 'Điện thoại từ 1-15 kí tự';
                    $flag = false;
                }
                if($this->_validate->validNickname($nickname)) {
                    $this->view->nicknameValue = $nickname;
                }
                else {
                    $this->view->errorNickname = 'Nickname từ 1-50 kí tự';
                    $flag = false;
                }
                
                if($flag == true) {
                    $data = array(
                        'id' => $id,
                        'name' => $name,
                        'phone' => $phone,
                        'nickname' => $nickname,
                        'type_nick' => $type_nick,
                    );
                    $this->_model->saveItem($data, 'update');
                    $this->_redirect('admincp/support');
                }
            }
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
		    $this->_model->deleteItem($id);
		    $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function publishAction()
		{
		    $id = (int) $this->_arrParam['id'];
		    $status = (int) $this->_arrParam['status'];
		    $status = ($status == 1) ? 2 : 1;
		    $data = array(
                'id' => $id,
		        'status' => $status,
		    );
		    $this->_model->saveItem($data, 'update');
		    $this->_redirect($_SERVER['HTTP_REFERER']);
		}
    }
